package com.hyzx.qbasic.admin.controller.process;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.ProcessConfigPO;
import com.hyzx.qbasic.admin.model.po.ProcessConfigPagedInfoPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.IProcessConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * Created by 黄江华 on 2018/11/28
 */


@Api(tags = "流程配置", description = "ProcessConfigController")
@Controller
@RequestMapping("/process/config")
public class ProcessConfigController extends BaseController {

    @Autowired
    private IProcessConfigService configService;

    @ApiIgnore
    @SysLog("流程配置首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.PROCESS_CONFIG_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.PROCESS_CONFIG_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.PROCESS_CONFIG_DELETE));
        return "process/config/list";
    }

    @ApiOperation("获取流程配置列表")
    @GetMapping("/getProcessConfigList")
    @ResponseBody
    public BaseResModel<PageInfo<ProcessConfigPagedInfoPO>> getProcessConfigList(@Valid GetProcessConfigListReq req, BindingResult result) {
        return BaseResUtils.success(configService.getPagedProcessConfigList(req));
    }

    @ApiIgnore
    @SysLog("新增流程配置页")
    @GetMapping("/add")
    public String add() {
        return "process/config/add";
    }

    @ApiOperation("新增流程配置表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddProcessConfigReq req, BindingResult result) {
        BaseOperationRes res = configService.addProcessConfig(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("删除流程配置")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteProcessConfigReq req, BindingResult result) {
        BaseOperationRes res = configService.deleteProcessConfig(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑流程配置页")
    @GetMapping("/edit")
    public String edit() {
        return "process/config/edit";
    }

    @ApiOperation("编辑流程配置表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditProcessConfigReq req, BindingResult result) {
        BaseOperationRes res = configService.editProcessConfig(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取流程配置信息")
    @GetMapping("/getProcessConfigInfo")
    @ResponseBody
    public BaseResModel<ProcessConfigPO> getProcessConfigInfo(@Valid GetProcessConfigInfoReq req, BindingResult result) {
        return BaseResUtils.success(configService.getProcessConfigById(req.getProcessConfigId()));
    }
}
